package com.cloudspace.yunloan.fragment;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudspace.yunloan.R;
import com.cloudspace.yunloan.constants.Constants;
import com.cloudspace.yunloan.entity.UploadMediaCallback;
import com.cloudspace.yunloan.mediarecorder.RecordActivity;
import com.cloudspace.yunloan.network.JsonCallBack;
import com.cloudspace.yunloan.network.api.API;
import com.cloudspace.yunloan.utils.AppPreferences;
import com.cloudspace.yunloan.utils.FileUtil;
import com.cloudspace.yunloan.utils.ToastMaker;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

public class PersonSignFragment extends BaseFragment implements View.OnClickListener {
    private View rootView;
    private ImageView signVideoImg;
    private RelativeLayout videoBgRl;
    private TextView mSaveVideoTv;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_person_sign, container, false);

        signVideoImg = rootView.findViewById(R.id.sign_video_img);
        signVideoImg.setOnClickListener(this);

        videoBgRl = rootView.findViewById(R.id.video_bg_rl);
        mSaveVideoTv = rootView.findViewById(R.id.save_video_tv);
        mSaveVideoTv.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onFragmentCreated(Bundle savedInstanceState) {

    }

    @Override
    public void updateContent() {

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_video_img){
            RecordActivity.showActivity(getActivity(), RecordActivity.class);
        }else if (v.getId() == R.id.save_video_tv){
            uploadVideo();
        }
    }

    private void uploadVideo() {
        String videoPath = AppPreferences.getString(Constants.VIDEO_PATH);
        File videoFile = new File(videoPath);
        if (TextUtils.isEmpty(videoPath) || !videoFile.exists()){
            ToastMaker.showToastShort(R.string.sign_video);
            return;
        }

        String cardId = AppPreferences.getString(Constants.CURRENT_CARD_ID);
        showProgressDialog("上传中...");
        API.getInstance().getUserApi().uploadMedia(getActivity(), cardId, videoFile, new JsonCallBack<UploadMediaCallback>() {
            @Override
            public void onSuccess(UploadMediaCallback uploadMediaCallback, Call call, Response response) {
                hideProgressDialog();
                if (uploadMediaCallback.status == 200){
                    ToastMaker.showToastShort("上传成功");
                    //删掉本地的签字视频文件
                    FileUtil.deleteFile(AppPreferences.getString(Constants.VIDEO_PATH));
                    AppPreferences.putString(Constants.VIDEO_PATH, "");
                    getActivity().finish();
                }else{
                    ToastMaker.showToastShort("上传出错，请稍后再试");
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                hideProgressDialog();
            }
        });
    }


    public  static Bitmap getVideoThumb(String path) {
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(path);
        return media.getFrameAtTime();
    }

    @Override
    public void onResume() {
        super.onResume();

        String videoPath = AppPreferences.getString(Constants.VIDEO_PATH);
        if (!TextUtils.isEmpty(videoPath)){
            Bitmap bm = getVideoThumb(videoPath);
            if (bm != null && videoBgRl != null){
                videoBgRl.setBackgroundDrawable(new BitmapDrawable(bm));
            }
        }
    }
}
